package java_DesignPattern.CreatorPattern.FactoryPattern.Config_Factory;

import java.util.HashMap;
import java.util.Properties;

/**
 * 咖啡工厂类
 */
public class CoffeeFactory {

    //初始化配置文件读取容器,key为咖啡全类名,value为咖啡对象
    private static HashMap<String,Coffe> coffeeMap = new HashMap<>();

    static {
        //读取配置文件
        Properties properties = new Properties();
        try {
            //读取数据
            properties.load(CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties"));
            for (Object key : properties.keySet()) {
                //根据key获取全类名
                String className = properties.getProperty((String) key);
                //通过反射全类名创建对象
                Class clazz = Class.forName(className);
                Coffe coffe = (Coffe) clazz.newInstance();

                //放入Map容器中
                coffeeMap.put((String) key,coffe);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Coffe createCoffe(String name){
        return coffeeMap.get(name);
    }
}
